﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Praxis.Entities;
using Praxis.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Praxis.UI
{
    public partial class FrmAlquiler : Form
    {

        [Dependency]
        public IAlquilerBL alquilerBL { get; set; }

        public FrmAlquiler()
        {
            InitializeComponent();
            this.BuildUp();
        }

        

        private void FrmAlquiler_Load(object sender, EventArgs e)
        {
            Cargar();
            aLQUILERBindingSource.AddNew();
        }

        private void Cargar()
        {
            aLQUILERBindingSource.DataSource = alquilerBL.GetAllAlquiler();
        }

        public ALQUILER CurrentAlquiler { get; set; }

        private void aLQUILERDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            CurrentAlquiler = aLQUILERBindingSource.Current as ALQUILER;
        }

        private void aLQUILERDataGridView_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            if (rbCliente.Checked)
                aLQUILERBindingSource.DataSource = alquilerBL.GetAlquilerByCliente(TxtDescripcion.Text);
            else
                if (rbObra.Checked)
                    aLQUILERBindingSource.DataSource = alquilerBL.GetAlquilerByObra(TxtDescripcion.Text);
            
        }

        private void BtnNuevo_Click_1(object sender, EventArgs e)
        {
            FrmMttoAlquiler frm = new FrmMttoAlquiler();
            frm.Nuevo();
            frm.Show();
        }

        private void TxtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rbCliente.Checked)
                    aLQUILERBindingSource.DataSource = alquilerBL.GetAlquilerByCliente(TxtDescripcion.Text);
                else
                    if (rbObra.Checked)
                        aLQUILERBindingSource.DataSource = alquilerBL.GetAlquilerByObra(TxtDescripcion.Text);
            }
        }

        private void rbCliente_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCliente.Checked)
            {
                TxtDescripcion.Text = "";
                TxtDescripcion.MaxLength = 50;
                Cargar();
            }
            else if (rbObra.Checked)
            {
                TxtDescripcion.Text = "";
                TxtDescripcion.MaxLength = 50;
                Cargar();
            }
        }

        private void TxtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
